package ro.myApp.security.entities;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.NotEmpty;

import ro.myApp.commons.basic.PersistentEntity;
import ro.myApp.commons.tree.TreeStructureComposit;
import ro.myApp.security.util.RoleEnum;

/**
 * @author anamaria
 * @version 1.0
 * @created 20-feb.-2010 01:12:13
 */
@Entity
@Table(name = "CUSTOM_ROLE", uniqueConstraints = { @UniqueConstraint(columnNames = "entity_path") })
public class Role 
	extends PersistentEntity implements TreeStructureComposit<Role> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6847183388928253329L;

	private boolean root;

	private boolean leaf;
	
	@NotEmpty
	@Column(name="entity_path", nullable = false)
	private String path;
	
	@Column(name="entity_order")
	private int order;
	
	@Column(name="entity_level")
	private int level;

	@Transient
	private Role parent;

	@Transient
	private List<Role> children = new LinkedList<Role>();

	@Enumerated(EnumType.STRING)
	@Column(nullable=true)
	private RoleEnum baseRole;

	public Role() {
		// TODO Auto-generated constructor stub
	}
	
	public Role(Long id, Integer version, boolean deleted, String code,
			String label, String description, String createdByUsername,
			String updatedByUsername, Date dateCreated, Date dateUpdated,
			String type, boolean root, boolean leaf, String path, int order,
			int level, Role parent, List<Role> children, RoleEnum baseRole) {
		
		super(id, version, deleted, code, label, description,
				createdByUsername, updatedByUsername, dateCreated, dateUpdated,
				type);
		this.root = root;
		this.leaf = leaf;
		this.path = path;
		this.order = order;
		this.level = level;
		this.parent = parent;
		this.children = children;
		this.baseRole = baseRole;
	}

	@Override
	public boolean isLeaf() {
		return this.leaf;
	}

	@Override
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	@Override
	public boolean isRoot() {
		return this.root;
	}

	@Override
	public void setRoot(boolean root) {
		this.root=root;
	}
	
	@Override
	public int getOrder() {
		return order;
	}
	
	@Override
	public void setOrder(int order) {
		this.order = order;
	}

	@Override
	public int getLevel() {
		return level;
	}
	
	@Override
	public void setLevel(int level) {
		this.level = level;
	}

	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}

	public RoleEnum getBaseRole() {
		return baseRole;
	}
	
	public void setBaseRole(RoleEnum baseRole) {
		this.baseRole = baseRole;
	}

	@Override
	public Role getParent() {
		return this.parent;
	}

	@Override
	public void setParent(Role parent) {
		this.parent = parent;
	}

	@Override
	public List<Role> getChildren() {
		return this.children;
	}

	@Override
	public void setChildren(List<Role> children) {
		this.children = children;
	}

	@Override
	public String toString() {
		return "Role [root=" + root + ", leaf=" + leaf + ", path=" + path
				+ ", order=" + order + ", level=" + level + "]";
	}
	
	
}